#include "ShowFindUpdateKR.h"


void ShowFindUpdateKR::Show(vector<Customer> cust_data)
{
        //Sort by name ascending
        std::sort(cust_data.begin(), cust_data.end(), compareCust);

        //Display customer data
        cout << "Here are the list of the customer sorted by name:" << endl;
        for(int i=0;i<cust_data.size();i++) cout << cust_data[i].display() << endl;
}

void ShowFindUpdateKR::Find(vector<Cart> cart_data, vector<Cart_item> cart_item_data, vector<Product> product_data)
{
        //Init variables
        int customer_id, total_sales = 0;

        //Get customer id first
        cout    << "Please input the customer ID you want to calculate the total sales: " << endl;
        cin     >> customer_id;

        //Check in all carts
        for(int i=0;i<cart_data.size();i++)
        {
                //If it's that customer
                if(cart_data[i].cust_get() == customer_id) 
                {
                        //Get cart id
                        int cart_id = cart_data[i].cart_get();
                        
                        //Get all item in cart
                        for(int j=0;j<cart_item_data.size();j++)
                        {
                                if (cart_item_data[j].cart_get() == cart_id)
                                {
                                        //Get the product and the quantity
                                        int product_id = cart_item_data[j].prod_get();
                                        int product_quantity = cart_item_data[j].qty_get();     
                                        double product_price = 0;

                                        //Get that product price
                                        for(int k=0;k<product_data.size();k++)
                                        {
                                                if (product_data[k].id_get() == product_id) 
                                                {
                                                        product_price = product_data[k].price_get();
                                                        break;
                                                }
                                        }

                                        //Add to sales
                                        total_sales += (product_quantity * product_price);
                                }
                        }
                }
        } 

        //Show the total sales          
        cout    << "Total sales of customer ID " << customer_id << " is: " << total_sales << endl;
}

void ShowFindUpdateKR::Update(vector<Customer>* cust_data)
{
        //Init input variables
        int id, addr_no, addr_postal;
        string name, addr_name;

        //Prompt user input
        cout    << "Please insert the customer data you want to insert!" << endl;
        cout    << "Customer ID: ";
        cin     >> id;
        cout    << "Customer Name: ";
        cin     >> name;
        cout    << "Customer Address No: ";
        cin     >> addr_no;
        cout    << "Customer Address Name: ";
        cin     >> addr_name;
        cout    << "Customer Address Postal: ";
        cin     >> addr_postal;

        //Just add it
		Street_address a		= Street_address(addr_no, addr_name, addr_postal);
        Customer cust			= Customer(id, name, a);
        cust_data->push_back(cust);

        //Log it
        cout    << "Customer data is successfully inserted." << endl;
}

bool ShowFindUpdateKR::compareCust(Customer const& c1, Customer const& c2)
{
        return c1.getName() < c2.getName();
}
